const utils = require('../../../../common/util/util')

Page({
  data: {
    label: '',
    id: '',
    readyMsg: 0,
    loading: true,
    page: 1,
    warning: true,
    list: []
  },
  onShareAppMessage: function () {
    // return {
    //   title: this.data.label
    // };
  },
  onLoad: function () {
    let that = this
    this.xInit()
  },
  xParReload:async function(){
    console.log(1)
  },
  // onReachBottom: function (e) {
  //   let that = this
  //   that.setData({
  //     warning: false,
  //     // loading : true
  //   })
  //   that.xInit()
  // },

  xTolist : function(){
    wx.navigateTo({
      url: '../list/index',
    })
  },


  xToIndex: function (e) {
    let id = e.currentTarget.dataset.id
    let label = e.currentTarget.dataset.label
    let mylike = e.currentTarget.dataset.mylike
    wx.navigateTo({
        url: '/subtool/pages/community/about/index?id=' + id + '&label=' + label + '&mylike=' + mylike,
    })
  },
  xInit: function () {
    let that = this
    let page = that.data.page
    let list = that.data.list
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "my_dynamic_content",
        page_number: page
      }
    }).then(res => {
      let data = res.result
      for (var i in data) {
        data[i].release_time = utils.formatTime_emotion(new Date(data[i].release_time))
        list.push(data[i])
      }
      that.setData({
          list: list,
          loading: false,
          warning: true
      })
      // if (data.length === 0) {
      //   that.setData({
      //     list: list,
      //     page: page,
      //     loading: false,
      //     warning: true
      //   })
      //   setTimeout(() => {
      //     that.setData({
      //       warning: true
      //     })
      //   }, 2000)
      // } else {
      //   page += 1
      //   that.setData({
      //     list: list,
      //     page: page,
      //     loading: false
      //   })
      // }
      wx.hideLoading()
    })
  },

  xToCreate: function () {
    let that = this
    let id = that.data.id
    let label = that.data.label
    wx.navigateTo({
        url: '/subtool/pages/community/index/index?id=' + id + '&label=' + label,
    })
  },
  //  日记练习
  xTopractice: function(){
    wx.navigateTo({
      url: '/subtool/pages/dayremb/index/index',
    })
  },
  xLike: function (e) {
    wx.showLoading({
      title: '刷新中',
    })
    let that = this
    let id = that.data.id
    let params = e.currentTarget.dataset
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "thumbs_up",
        _id: params.id,
      }
    }).then(res => {
      that.setData({
        [`list[${params.index}].me_thumbs_up`]: !params.state,
        [`list[${params.index}].total_thumbs_up`]: !params.state ? params.key + 1 : params.key - 1
      })
      if (!params.state) {
        wx.showToast({
          title: '点赞成功',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '取消成功',
          icon: 'none'
        })
      }
    })
  },

  xToDetail: function (e) {
    let id = e.currentTarget.dataset.id
    let openid = e.currentTarget.dataset.openid
    let totalcomment = e.currentTarget.dataset.totalcomment
    console.log(totalcomment)
    wx.navigateTo({
      url: '/subtool/pages/community/detail/index?id=' + id + '&openid=' + openid +'&totalcomment='+ totalcomment,
    })
  },


  xToNewMessage: function () {
    wx.navigateTo({
      url: '/subtool/pages/community/msg/index',
    })
  },

  back: function () {
    wx.navigateBack({
      delta: -1,
    })
  },

  xMyLike: function () {
    let that = this
    let id = that.data.id
    let _dataObj = {
      group_id: id
    }
    if(that.data.mylike==0){
      wx.showLoading({
        title: '关注中',
      })
    }else{
      wx.showLoading({
        title: '取关中',
      })
    }
    wx.cloud.callFunction({
      name: 'dynamic_content',
      data: {
        action: "follow_group",
        dataObj: _dataObj
      }
    }).then(res => {
      console.log("返回:", res.result)
      that.setData({
        mylike : that.data.mylike ==1 ? 0 : 1
      })
      wx.hideLoading()
    })
  }
})